/** 
 * Project Name:zju 
 * File Name:AdminController.java 
 * Package Name:cn.edu.zju.controller 
 * Date:2014年10月9日下午2:00:58 
 * Copyright (c) 2014, 杭州杰唐信息技术有限公司 All Rights Reserved. 
 * 
 */
package com.hbjycl.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.TaskExecutor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;





import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hbjycl.entity.Admin;
import com.hbjycl.service.AdminService;
import com.hbjycl.service.RoleService;

/**
 * 用户管理
 * 
 * @author wxp
 *
 * @datetime 2014年10月9日下午2:00:58
 */
@Controller
public class AdminController extends BaseController
{
    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);
    
    @Resource
    private RoleService         roleService;
    @Resource
    private AdminService        adminService;
   

 
    
    @RequestMapping(value = "/user/info", method = RequestMethod.GET)
    public ModelAndView info(HttpSession session, HttpServletRequest request)
    {
        
        ModelAndView mav = new ModelAndView();
        mav.setViewName("front/userinfo");
        
        addUser(mav);
        Admin admin = getLoginUser();
        if (admin == null)
        {
            mav.setViewName("redirect:/");
            return mav;
        }
        Admin user = adminService.get(admin.getId());
        mav.addObject("user", user);
        return mav;
        
    }
    
    @RequestMapping(value = "/user/info/changepwd", method = RequestMethod.POST)
    public ModelAndView changePwd(
            @RequestParam(value = "oldpwd", required = false, defaultValue = "") String oldpwd,
            @RequestParam(value = "newpwd", required = false, defaultValue = "") String newpwd)
    {
        
        ModelAndView mav = new ModelAndView();
        mav.setViewName("front/userinfo");
        addUser(mav);
        Admin admin = getLoginUser();
        if (admin == null)
        {
            mav.setViewName("redirect:/");
            return mav;
        }
        if (!StringUtils.equals(admin.getPassword(), oldpwd))
        {
            mav.addObject(TIP_ERROR, "原密码错误！");
        }
        else
        {
            admin.setPassword(newpwd);
            adminService.update(admin);
            mav.addObject(TIP_INFO, "密码修改成功！");
        }
        Admin user = adminService.get(admin.getId());
        mav.addObject("user", user);
        return mav;
    }
    
    @RequestMapping(value = "/user/info/update", method = RequestMethod.POST)
    public ModelAndView updateInfo(Admin formAdmin,
            @RequestParam(value = "password", required = false, defaultValue = "") String password,
            @RequestParam(value = "nickname", required = false, defaultValue = "") String nickname)
    {
        
        ModelAndView mav = new ModelAndView();
        mav.setViewName("front/userinfo");
        
        addUser(mav);
        Admin admin = getLoginUser();
        if (admin == null)
        {
            mav.setViewName("redirect:/");
            return mav;
        }
        formAdmin.setName(nickname);
        Admin ad = adminService.get(formAdmin.getId());
        if (ad == null)
        {
            mav.setViewName("redirect:/");
            return mav;
        }
        
        ad.setEmail(formAdmin.getEmail());
        ad.setName(formAdmin.getName());
        ad.setPhone(formAdmin.getPhone());
        ad.setMobile(formAdmin.getMobile());
        ad.setDepartment(formAdmin.getDepartment());
       
        if (password != null && password.trim().length() > 0)
        {
            ad.setPassword(password);
        }
        ad.setModifyDate(new Date());
        boolean result = false;
        
        try
        {
            admin.setName(formAdmin.getName());
            adminService.update(ad);
            result = true;
            
        }
        catch (Exception e)
        {
            result = false;
            e.printStackTrace();
        }
        if (result)
        {
            mav.addObject(TIP_INFO, "更新成功!");
            mav.addObject("user", ad);
            return mav;
        }
        mav.addObject(TIP_ERROR, "更新失败！");
        mav.addObject("user", ad);
        
        return mav;
        
    }
    
    @RequestMapping(value = "/user/comment", method = RequestMethod.GET)
    public ModelAndView comment(
            @RequestParam(value = "pageNumber", required = false, defaultValue = "1") int pageNumber,
            @RequestParam(value = "pageSize", required = false, defaultValue = "20") int pageSize)
    {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("front/comment");
        addUser(mav);
        Admin admin = getLoginUser();
        if (admin == null)
        {
            mav.setViewName("common/redirect");
            mav.addObject("msg1",
                    "用户未登录，马上<span style='color:red'><a href='accounts/login'>登录</span>");
            mav.addObject("msg2", "即将返回主页");
            // mav.addObject("redirect", "");
            return mav;
        }
        return mav;
    }
    
    @RequestMapping(value = "/user/home", method = RequestMethod.GET)
    public ModelAndView index(HttpSession session, HttpServletRequest request)
    {
        ModelAndView mav = new ModelAndView();
        
        if (getLoginUser() == null)
        {
            mav.setViewName("redirect:/accounts/login");
            return mav;
        }
        
        mav.setViewName("front/userhome");
        addUser(mav);
        
        Admin admin = getLoginUser();
        if (admin == null)
        {
            mav.setViewName("redirect:/");
            return mav;
        }
        StringBuilder sb = new StringBuilder();
        logger.info(sb.toString());        
        mav.addObject("folderStr", sb.toString());
        mav.addObject("id", "notneed");
       return mav;
    }
  
    
}
